package com.joplus.housekeeper.adapter;

import android.content.Context;
import android.view.Gravity;
import android.view.View;
import android.widget.FrameLayout;

import com.baiiu.filter.adapter.MenuAdapter;
import com.baiiu.filter.adapter.SimpleTextAdapter;
import com.baiiu.filter.interfaces.OnFilterDoneListener;
import com.baiiu.filter.typeview.DoubleListView;
import com.baiiu.filter.util.CommonUtil;
import com.baiiu.filter.util.UIUtil;
import com.baiiu.filter.view.FilterCheckedTextView;
import com.joplus.housekeeper.R;
import com.joplus.housekeeper.utils.FilterType;
import com.joplus.housekeeper.utils.FilterUrl;
import com.joplus.housekeeper.utils.StringUtils;

import java.util.List;

/**
 * author： ccm
 * time： 2017/5/2.
 * description：
 */

public class DropMenuAdapter implements MenuAdapter {
    private Context mContext;
    private String[] titleList;
    private OnFilterDoneListener onFilterDoneListener;
    private DoubleListView<FilterType, String> areaView;

    private DoubleListView<FilterType, String> department;

    public DropMenuAdapter(Context context, String[] titleList, OnFilterDoneListener onFilterDoneListener) {
        this.mContext = context;
        this.titleList = titleList;
        this.onFilterDoneListener = onFilterDoneListener;
    }

    @Override
    public int getMenuCount() {
        return titleList.length;
    }

    @Override
    public String getMenuTitle(int position) {
        return titleList[position];
    }

    @Override
    public int getBottomMargin(int position) {
        if (position == 2) {
            return 0;
        }

        return UIUtil.dp(mContext, 140);
    }

    @Override
    public View getView(int position, FrameLayout parentContainer) {
        View view = parentContainer.getChildAt(position);
        switch (position) {
            case 0:
                view = createArea();
                break;

            case 1:
                view = createDapartment();
                break;

        }

        return view;
    }


    private View createArea() {


        areaView = new DoubleListView<FilterType, String>(mContext)
                .leftAdapter(new SimpleTextAdapter<FilterType>(null, mContext) {
                    @Override
                    public String provideText(FilterType filterType) {
                        return filterType.desc;
                    }

                    @Override
                    protected void initCheckedTextView(FilterCheckedTextView checkedTextView) {
                        checkedTextView.setPadding(UIUtil.dp(mContext, 20), UIUtil.dp(mContext, 15), 0, UIUtil.dp(mContext, 15));
                        checkedTextView.setGravity(Gravity.LEFT | Gravity.CENTER_VERTICAL);
                    }
                })
                .rightAdapter(new SimpleTextAdapter<String>(null, mContext) {
                    @Override
                    public String provideText(String s) {
                        return s;
                    }

                    @Override
                    protected void initCheckedTextView(FilterCheckedTextView checkedTextView) {
                        checkedTextView.setPadding(UIUtil.dp(mContext, 20), UIUtil.dp(mContext, 15), 0, UIUtil.dp(mContext, 15));
                        checkedTextView.setGravity(Gravity.LEFT | Gravity.CENTER_VERTICAL);
                        checkedTextView.setBackgroundResource(android.R.color.white);
                    }
                })
                .onLeftItemClickListener(new DoubleListView.OnLeftItemClickListener<FilterType, String>() {
                    @Override
                    public List<String> provideRightList(FilterType item, int position) {
                        List<String> child = item.child;
                        if (CommonUtil.isEmpty(child)) {
                            FilterUrl.instance().doubleListLeft = item.parentId;
                            FilterUrl.instance().doubleListRight = "";
                            FilterUrl.instance().position = 0;
                            FilterUrl.instance().positionTitle = item.desc;
                            onFilterDone();
                        }

                        return child;
                    }
                })
                .onRightItemClickListener(new DoubleListView.OnRightItemClickListener<FilterType, String>() {
                    @Override
                    public void onRightItemClick(FilterType item, int position) {
                        List<String> child = item.child;
                        List<String> childId = item.childId;
                        FilterUrl.instance().doubleListLeft = item.parentId;
                        FilterUrl.instance().doubleListRight = childId.get(position);
                        FilterUrl.instance().position = 0;
                        if (StringUtils.isEmpty(childId.get(position))) {
                            FilterUrl.instance().positionTitle = item.desc;
                        } else {

                            FilterUrl.instance().positionTitle = child.get(position);
                        }


                        onFilterDone();
                    }
                });
        return areaView;
    }

    public static final int TYPE_AREA = 0;
    public static final int TYPE_DEPARTMENT = 1;

    private View createDapartment() {
        department = new DoubleListView<FilterType, String>(mContext)
                .leftAdapter(new SimpleTextAdapter<FilterType>(null, mContext) {
                    @Override
                    public String provideText(FilterType filterType) {
                        return filterType.desc;
                    }

                    @Override
                    protected void initCheckedTextView(FilterCheckedTextView checkedTextView) {
                        checkedTextView.setPadding(UIUtil.dp(mContext, 20), UIUtil.dp(mContext, 15), 0, UIUtil.dp(mContext, 15));
                        checkedTextView.setGravity(Gravity.LEFT | Gravity.CENTER_VERTICAL);
                    }
                })
                .rightAdapter(new SimpleTextAdapter<String>(null, mContext) {
                    @Override
                    public String provideText(String s) {
                        return s;
                    }

                    @Override
                    protected void initCheckedTextView(FilterCheckedTextView checkedTextView) {
                        checkedTextView.setPadding(UIUtil.dp(mContext, 20), UIUtil.dp(mContext, 15), 0, UIUtil.dp(mContext, 15));
                        checkedTextView.setGravity(Gravity.LEFT | Gravity.CENTER_VERTICAL);
                        checkedTextView.setBackgroundResource(android.R.color.white);
                    }
                })
                .onLeftItemClickListener(new DoubleListView.OnLeftItemClickListener<FilterType, String>() {
                    @Override
                    public List<String> provideRightList(FilterType item, int position) {
                        List<String> child = item.child;

                        if (CommonUtil.isEmpty(child)) {
                            FilterUrl.instance().doubleListLeft = item.desc;
                            FilterUrl.instance().parentId = item.parentId;
                            FilterUrl.instance().doubleListRight = "";

                            FilterUrl.instance().position = 1;
                            FilterUrl.instance().positionTitle = item.desc;

                            onFilterDone();
                        }

                        return child;
                    }
                })
                .onRightItemClickListener(new DoubleListView.OnRightItemClickListener<FilterType, String>() {
                    @Override
                    public void onRightItemClick(FilterType item, int position) {
                        List<String> childId = item.childId;
                        List<String> child = item.child;
                        FilterUrl.instance().doubleListLeft = item.parentId;
                        FilterUrl.instance().doubleListRight = childId.get(position);
                        FilterUrl.instance().childId = childId.get(position);

                        FilterUrl.instance().position = 1;
                        if (StringUtils.isEmpty(childId.get(position))) {
                            FilterUrl.instance().positionTitle = item.desc;
                        } else {
                            FilterUrl.instance().positionTitle = child.get(position);
                        }


                        onFilterDone();
                    }
                });
        return department;
    }

    private void onFilterDone() {
        if (onFilterDoneListener != null) {
            onFilterDoneListener.onFilterDone(0, "", "");
        }
    }

    public void setdepartment(List<FilterType> list) {
        department.setLeftList(list, 0);
        department.setRightList(list.get(0).child, -1);
        department.getLeftListView().setBackgroundColor(mContext.getResources().getColor(R.color.b_c_fafafa));
    }

    public void setArea(List<FilterType> list) {
        areaView.setLeftList(list, 0);
        areaView.setRightList(list.get(0).child, -1);
        areaView.getLeftListView().setBackgroundColor(mContext.getResources().getColor(R.color.b_c_fafafa));
    }
}
